package cn.csg.iotplatform.module.system.controller.admin.user.vo.user;

import cn.csg.iotplatform.framework.common.util.validation.ValidationUtils;
import cn.csg.iotplatform.framework.common.validation.PasswordStrength;
import cn.hutool.core.util.ObjectUtil;
import cn.csg.iotplatform.framework.common.validation.Mobile;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.*;
import java.time.LocalDateTime;
import java.util.Set;

@Schema(description = "管理后台 - 用户创建/修改 Request VO")
@Data
public class UserSaveReqVO {

    @Schema(description = "用户编号", example = "1024")
    private Long id;

    @Schema(description = "用户账号", requiredMode = Schema.RequiredMode.REQUIRED, example = "iot")
    @NotBlank(message = "用户账号不能为空")
    @Pattern(regexp = ValidationUtils.PATTERN_USER_ACCOUNT_STR, message = "用户账号由 数字、字母 组成")
    @Size(min = 4, max = 30, message = "用户账号长度为 4-30 个字符")
    private String username;

    @Schema(description = "用户昵称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋艿")
    @Size(max = 30, message = "用户昵称长度不能超过30个字符")
    private String nickname;

    @Schema(description = "备注", example = "我是一个用户")
    private String remark;

    @Schema(description = "部门ID", example = "我是一个用户")
    private Long deptId;

    @Schema(description = "岗位编号数组", example = "1")
    private Set<Long> postIds;

    @Schema(description = "用户邮箱", example = "iot@iocoder.cn")
    @Email(message = "邮箱格式不正确")
    @Size(max = 50, message = "邮箱长度不能超过 50 个字符")
    private String email;

    @Schema(description = "手机号码", example = "15601691300")
    @Mobile
    private String mobile;

    @Schema(description = "用户性别，参见 SexEnum 枚举类", example = "1",defaultValue = "null")
    private Integer sex;

    @Schema(description = "用户头像", example = "https://www.iocoder.cn/xxx.png")
    private String avatar;

    @Schema(description = "elink帐号", requiredMode = Schema.RequiredMode.REQUIRED, example = "elink帐号")
    private String elink;

    @Schema(description = "是否有效(0-失效、1-有效)", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer isValid;

    @Schema(description = "禁用原因", example = "用户超过X天未登录系统自动禁用")
    private String disableReason;

    @Schema(description = "失效日期", requiredMode = Schema.RequiredMode.REQUIRED, example = "时间戳格式")
    private LocalDateTime expirationDate;

    @Schema(description = "绑定ip地址", example = "127.0.0.1;127.0.0.1")
    private String userIp;

    @Schema(description = "借调单位ids", example = "1")
    private Set<Long> relationDeptIds;

    @Schema(description = "工号", example = "1")
    private String userNo;

    @Schema(description = "租户类型(0-非租户、1-租户)", example = "0")
    private Integer isTenant;

    // ========== 仅【创建】时，需要传递的字段 ==========

    @Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "Test123456")
    @PasswordStrength(minLength = 8, maxLength = 16, message = "密码长度8-16位，且必须包含字母数字特殊字符中两种")
    private String password;

    @AssertTrue(message = "密码不能为空")
    @JsonIgnore
    public boolean isPasswordValid() {
        return id != null // 修改时，不需要传递
                || (ObjectUtil.isAllNotEmpty(password)); // 新增时，必须都传递 password
    }

    @JsonIgnore
    private String roleNames;
    @JsonIgnore
    private String deptName;

    /**
     * 是否首次登录(0-否、1-是)
     */
    @Schema(description = "是否首次登录(0-否、1-是)", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer firLogin;

    /**
     * 客户端id
     */
    private String clientId;

    /**
     * 加密后的参数数据
     */
    private String encryptionData;

}
